{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.2"
    },
    "colab": {
      "name": "Bert.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "accelerator": "GPU",
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "2bf939cf51634a2891810201a22b3ed6": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "state": {
            "_view_name": "HBoxView",
            "_dom_classes": [],
            "_model_name": "HBoxModel",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "box_style": "",
            "layout": "IPY_MODEL_8b575ac374044f919678aa4239d0e118",
            "_model_module": "@jupyter-widgets/controls",
            "children": [
              "IPY_MODEL_f9ddd85e5db144f4a102245a4be9bb15",
              "IPY_MODEL_69c86dd28d3549969f18721bb7543047"
            ]
          }
        },
        "8b575ac374044f919678aa4239d0e118": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "f9ddd85e5db144f4a102245a4be9bb15": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "state": {
            "_view_name": "ProgressView",
            "style": "IPY_MODEL_9f77377124ea4ad29aa936d83132e881",
            "_dom_classes": [],
            "description": "models/ProtBert/pytorch_model.bin: 100%",
            "_model_name": "FloatProgressModel",
            "bar_style": "success",
            "max": 1684058277,
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": 1684058277,
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "orientation": "horizontal",
            "min": 0,
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_0601b95b5292494e86d450032e13b027"
          }
        },
        "69c86dd28d3549969f18721bb7543047": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_9c01724ea4e84ff49652981c9b3e30ce",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": " 1.57G/1.57G [01:27&lt;00:00, 19.1MB/s]",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_82e683b99bd04caabd691bbe16415aa6"
          }
        },
        "9f77377124ea4ad29aa936d83132e881": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "ProgressStyleModel",
            "description_width": "initial",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "bar_color": null,
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "0601b95b5292494e86d450032e13b027": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "9c01724ea4e84ff49652981c9b3e30ce": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "82e683b99bd04caabd691bbe16415aa6": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "93c5aae269c248f49a71b93fa83f6e59": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "state": {
            "_view_name": "HBoxView",
            "_dom_classes": [],
            "_model_name": "HBoxModel",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "box_style": "",
            "layout": "IPY_MODEL_ee5b6d118ae540bf9e3479e678238f12",
            "_model_module": "@jupyter-widgets/controls",
            "children": [
              "IPY_MODEL_54bddeac0f8d4a2a87f74395e9415e12",
              "IPY_MODEL_d37c2a32624a401489edc1af937cd066"
            ]
          }
        },
        "ee5b6d118ae540bf9e3479e678238f12": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "54bddeac0f8d4a2a87f74395e9415e12": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "state": {
            "_view_name": "ProgressView",
            "style": "IPY_MODEL_5aa8ef77c1d046d3a9373ed1e050caaa",
            "_dom_classes": [],
            "description": "models/ProtBert/config.json: 100%",
            "_model_name": "FloatProgressModel",
            "bar_style": "success",
            "max": 313,
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": 313,
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "orientation": "horizontal",
            "min": 0,
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_d0287581330d4a42be15d24f8c264824"
          }
        },
        "d37c2a32624a401489edc1af937cd066": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_b7392b1ae6244e298440be33e03997c5",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": " 313/313 [00:01&lt;00:00, 242B/s]",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_f54642c4dc654187abcb92a95e25a9d7"
          }
        },
        "5aa8ef77c1d046d3a9373ed1e050caaa": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "ProgressStyleModel",
            "description_width": "initial",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "bar_color": null,
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "d0287581330d4a42be15d24f8c264824": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "b7392b1ae6244e298440be33e03997c5": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "f54642c4dc654187abcb92a95e25a9d7": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "5385f58962f64a528d2f36d84249cd93": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HBoxModel",
          "state": {
            "_view_name": "HBoxView",
            "_dom_classes": [],
            "_model_name": "HBoxModel",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "box_style": "",
            "layout": "IPY_MODEL_bf4a390b55464ad094706fb8b8f35744",
            "_model_module": "@jupyter-widgets/controls",
            "children": [
              "IPY_MODEL_337be596930f457d85b1bb1f3aa9ba55",
              "IPY_MODEL_f3fed8cae573468b8876cbbfe9da25a3"
            ]
          }
        },
        "bf4a390b55464ad094706fb8b8f35744": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "337be596930f457d85b1bb1f3aa9ba55": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "FloatProgressModel",
          "state": {
            "_view_name": "ProgressView",
            "style": "IPY_MODEL_1a446dfe426944f29a59c9fb8f7cae65",
            "_dom_classes": [],
            "description": "models/ProtBert/vocab.txt: 100%",
            "_model_name": "FloatProgressModel",
            "bar_style": "success",
            "max": 81,
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": 81,
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "orientation": "horizontal",
            "min": 0,
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_bb0e6825d00249129b6db2708f84efb9"
          }
        },
        "f3fed8cae573468b8876cbbfe9da25a3": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "HTMLModel",
          "state": {
            "_view_name": "HTMLView",
            "style": "IPY_MODEL_0f5eefcbe879470bb13fd84c8c78c281",
            "_dom_classes": [],
            "description": "",
            "_model_name": "HTMLModel",
            "placeholder": "​",
            "_view_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "value": " 81.0/81.0 [00:03&lt;00:00, 26.4B/s]",
            "_view_count": null,
            "_view_module_version": "1.5.0",
            "description_tooltip": null,
            "_model_module": "@jupyter-widgets/controls",
            "layout": "IPY_MODEL_250af8ec98654827a917f86f587b927b"
          }
        },
        "1a446dfe426944f29a59c9fb8f7cae65": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "ProgressStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "ProgressStyleModel",
            "description_width": "initial",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "bar_color": null,
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "bb0e6825d00249129b6db2708f84efb9": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        },
        "0f5eefcbe879470bb13fd84c8c78c281": {
          "model_module": "@jupyter-widgets/controls",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_view_name": "StyleView",
            "_model_name": "DescriptionStyleModel",
            "description_width": "",
            "_view_module": "@jupyter-widgets/base",
            "_model_module_version": "1.5.0",
            "_view_count": null,
            "_view_module_version": "1.2.0",
            "_model_module": "@jupyter-widgets/controls"
          }
        },
        "250af8ec98654827a917f86f587b927b": {
          "model_module": "@jupyter-widgets/base",
          "model_name": "LayoutModel",
          "state": {
            "_view_name": "LayoutView",
            "grid_template_rows": null,
            "right": null,
            "justify_content": null,
            "_view_module": "@jupyter-widgets/base",
            "overflow": null,
            "_model_module_version": "1.2.0",
            "_view_count": null,
            "flex_flow": null,
            "width": null,
            "min_width": null,
            "border": null,
            "align_items": null,
            "bottom": null,
            "_model_module": "@jupyter-widgets/base",
            "top": null,
            "grid_column": null,
            "overflow_y": null,
            "overflow_x": null,
            "grid_auto_flow": null,
            "grid_area": null,
            "grid_template_columns": null,
            "flex": null,
            "_model_name": "LayoutModel",
            "justify_items": null,
            "grid_row": null,
            "max_height": null,
            "align_content": null,
            "visibility": null,
            "align_self": null,
            "height": null,
            "min_height": null,
            "padding": null,
            "grid_auto_rows": null,
            "grid_gap": null,
            "max_width": null,
            "order": null,
            "_view_module_version": "1.2.0",
            "grid_template_areas": null,
            "object_position": null,
            "object_fit": null,
            "grid_auto_columns": null,
            "margin": null,
            "display": null,
            "left": null
          }
        }
      }
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/agemagician/Prot-Transformers/blob/master/Benchmark/Bert.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jKLvLEnt0aw7",
        "colab_type": "text"
      },
      "source": [
        "<h3> Benchmark ProtBert Model using GPU or CPU <h3>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3jXYUooT0aw9",
        "colab_type": "text"
      },
      "source": [
        "<b>1. Load necessry libraries including huggingface transformers<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DpoYBdbs0jSS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install -q transformers"
      ],
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GnGZuNIm0axA",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch\n",
        "from transformers import BertModel\n",
        "import time\n",
        "from datetime import timedelta\n",
        "import os\n",
        "import requests\n",
        "from tqdm.auto import tqdm"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "XqmVMZYw0axG",
        "colab_type": "text"
      },
      "source": [
        "<b>2. Set the url location of ProtBert and the vocabulary file<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "D9-pJtmU0axH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "modelUrl = 'https://www.dropbox.com/s/dm3m1o0tsv9terq/pytorch_model.bin?dl=1'\n",
        "configUrl = 'https://www.dropbox.com/s/d3yw7v4tvi5f4sk/bert_config.json?dl=1'\n",
        "vocabUrl = 'https://www.dropbox.com/s/jvrleji50ql5m5i/vocab.txt?dl=1'"
      ],
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Cv_-QMOE1EME",
        "colab_type": "text"
      },
      "source": [
        "<b>3. Download ProtBert models and vocabulary files<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GpJpbyfJ1LSU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "downloadFolderPath = 'models/ProtBert/'"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hhrYlqAu1LbS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "modelFolderPath = downloadFolderPath\n",
        "\n",
        "modelFilePath = os.path.join(modelFolderPath, 'pytorch_model.bin')\n",
        "\n",
        "configFilePath = os.path.join(modelFolderPath, 'config.json')\n",
        "\n",
        "vocabFilePath = os.path.join(modelFolderPath, 'vocab.txt')"
      ],
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9RgoS6bj1Lga",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "if not os.path.exists(modelFolderPath):\n",
        "    os.makedirs(modelFolderPath)"
      ],
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5nQI_lPu1ZBx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def download_file(url, filename):\n",
        "  response = requests.get(url, stream=True)\n",
        "  with tqdm.wrapattr(open(filename, \"wb\"), \"write\", miniters=1,\n",
        "                    total=int(response.headers.get('content-length', 0)),\n",
        "                    desc=filename) as fout:\n",
        "      for chunk in response.iter_content(chunk_size=4096):\n",
        "          fout.write(chunk)"
      ],
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "c7UfPSyL1ZHM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 164,
          "referenced_widgets": [
            "2bf939cf51634a2891810201a22b3ed6",
            "8b575ac374044f919678aa4239d0e118",
            "f9ddd85e5db144f4a102245a4be9bb15",
            "69c86dd28d3549969f18721bb7543047",
            "9f77377124ea4ad29aa936d83132e881",
            "0601b95b5292494e86d450032e13b027",
            "9c01724ea4e84ff49652981c9b3e30ce",
            "82e683b99bd04caabd691bbe16415aa6",
            "93c5aae269c248f49a71b93fa83f6e59",
            "ee5b6d118ae540bf9e3479e678238f12",
            "54bddeac0f8d4a2a87f74395e9415e12",
            "d37c2a32624a401489edc1af937cd066",
            "5aa8ef77c1d046d3a9373ed1e050caaa",
            "d0287581330d4a42be15d24f8c264824",
            "b7392b1ae6244e298440be33e03997c5",
            "f54642c4dc654187abcb92a95e25a9d7",
            "5385f58962f64a528d2f36d84249cd93",
            "bf4a390b55464ad094706fb8b8f35744",
            "337be596930f457d85b1bb1f3aa9ba55",
            "f3fed8cae573468b8876cbbfe9da25a3",
            "1a446dfe426944f29a59c9fb8f7cae65",
            "bb0e6825d00249129b6db2708f84efb9",
            "0f5eefcbe879470bb13fd84c8c78c281",
            "250af8ec98654827a917f86f587b927b"
          ]
        },
        "outputId": "de11ba9a-6e64-43ae-a97c-3030094020aa"
      },
      "source": [
        "if not os.path.exists(modelFilePath):\n",
        "    download_file(modelUrl, modelFilePath)\n",
        "\n",
        "if not os.path.exists(configFilePath):\n",
        "    download_file(configUrl, configFilePath)\n",
        "\n",
        "if not os.path.exists(vocabFilePath):\n",
        "    download_file(vocabUrl, vocabFilePath)"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "2bf939cf51634a2891810201a22b3ed6",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "HBox(children=(FloatProgress(value=0.0, description='models/ProtBert/pytorch_model.bin', max=1684058277.0, sty…"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "93c5aae269c248f49a71b93fa83f6e59",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "HBox(children=(FloatProgress(value=0.0, description='models/ProtBert/config.json', max=313.0, style=ProgressSt…"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "5385f58962f64a528d2f36d84249cd93",
              "version_minor": 0,
              "version_major": 2
            },
            "text/plain": [
              "HBox(children=(FloatProgress(value=0.0, description='models/ProtBert/vocab.txt', max=81.0, style=ProgressStyle…"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DA8VJMAk0axN",
        "colab_type": "text"
      },
      "source": [
        "<b>4. Load ProtBert Model<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hjXcKjqg0axP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = BertModel.from_pretrained(modelFolderPath)"
      ],
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hKD2Qvde0axT",
        "colab_type": "text"
      },
      "source": [
        "<b>5. Load the model into the GPU if avilabile and switch to inference mode<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ksASffyj0axU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')"
      ],
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hxf0RX9o0axX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = model.to(device)\n",
        "model = model.eval()"
      ],
      "execution_count": 13,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JGRdbkad0axb",
        "colab_type": "text"
      },
      "source": [
        "<b>6. Benchmark Configuration<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JFkGZ1gT0axl",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "min_batch_size = 8\n",
        "max_batch_size = 32\n",
        "inc_batch_size = 8\n",
        "\n",
        "min_sequence_length = 64\n",
        "max_sequence_length = 512\n",
        "inc_sequence_length = 64\n",
        "\n",
        "iterations = 10"
      ],
      "execution_count": 14,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "atUiO9Y10axr",
        "colab_type": "text"
      },
      "source": [
        "<b>7. Start Benchmarking<b>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xbAIdSYl0axs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 748
        },
        "outputId": "072af120-6da7-4bbc-ab2a-a45e407e8e70"
      },
      "source": [
        "device_name = torch.cuda.get_device_name(device.index) if device.type == 'cuda' else 'CPU'\n",
        "\n",
        "with torch.no_grad():\n",
        "    print((' Benchmarking using ' + device_name + ' ').center(80, '*'))\n",
        "    print(' Start '.center(80, '*'))\n",
        "    for sequence_length in range(min_sequence_length,max_sequence_length+1,inc_sequence_length):\n",
        "        for batch_size in range(min_batch_size,max_batch_size+1,inc_batch_size):\n",
        "            start = time.time()\n",
        "            for i in range(iterations):\n",
        "                input_ids = torch.randint(1, 20, (batch_size,sequence_length)).to(device)\n",
        "                results = model(input_ids)[0].cpu().numpy()\n",
        "            end = time.time()\n",
        "            ms_per_protein = (end-start)/(iterations*batch_size)\n",
        "            print('Sequence Length: %4d \\t Batch Size: %4d \\t Ms per protein %4.2f' %(sequence_length,batch_size,ms_per_protein))\n",
        "        print(' Done '.center(80, '*'))\n",
        "    print(' Finished '.center(80, '*'))"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "******************* Benchmarking using Tesla P100-PCIE-16GB ********************\n",
            "************************************ Start *************************************\n",
            "Sequence Length:   64 \t Batch Size:    8 \t Ms per protein 0.01\n",
            "Sequence Length:   64 \t Batch Size:   16 \t Ms per protein 0.01\n",
            "Sequence Length:   64 \t Batch Size:   24 \t Ms per protein 0.01\n",
            "Sequence Length:   64 \t Batch Size:   32 \t Ms per protein 0.01\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  128 \t Batch Size:    8 \t Ms per protein 0.02\n",
            "Sequence Length:  128 \t Batch Size:   16 \t Ms per protein 0.02\n",
            "Sequence Length:  128 \t Batch Size:   24 \t Ms per protein 0.02\n",
            "Sequence Length:  128 \t Batch Size:   32 \t Ms per protein 0.02\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  192 \t Batch Size:    8 \t Ms per protein 0.02\n",
            "Sequence Length:  192 \t Batch Size:   16 \t Ms per protein 0.02\n",
            "Sequence Length:  192 \t Batch Size:   24 \t Ms per protein 0.02\n",
            "Sequence Length:  192 \t Batch Size:   32 \t Ms per protein 0.02\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  256 \t Batch Size:    8 \t Ms per protein 0.04\n",
            "Sequence Length:  256 \t Batch Size:   16 \t Ms per protein 0.03\n",
            "Sequence Length:  256 \t Batch Size:   24 \t Ms per protein 0.03\n",
            "Sequence Length:  256 \t Batch Size:   32 \t Ms per protein 0.03\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  320 \t Batch Size:    8 \t Ms per protein 0.04\n",
            "Sequence Length:  320 \t Batch Size:   16 \t Ms per protein 0.04\n",
            "Sequence Length:  320 \t Batch Size:   24 \t Ms per protein 0.04\n",
            "Sequence Length:  320 \t Batch Size:   32 \t Ms per protein 0.04\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  384 \t Batch Size:    8 \t Ms per protein 0.05\n",
            "Sequence Length:  384 \t Batch Size:   16 \t Ms per protein 0.05\n",
            "Sequence Length:  384 \t Batch Size:   24 \t Ms per protein 0.05\n",
            "Sequence Length:  384 \t Batch Size:   32 \t Ms per protein 0.05\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  448 \t Batch Size:    8 \t Ms per protein 0.06\n",
            "Sequence Length:  448 \t Batch Size:   16 \t Ms per protein 0.06\n",
            "Sequence Length:  448 \t Batch Size:   24 \t Ms per protein 0.06\n",
            "Sequence Length:  448 \t Batch Size:   32 \t Ms per protein 0.06\n",
            "************************************* Done *************************************\n",
            "Sequence Length:  512 \t Batch Size:    8 \t Ms per protein 0.07\n",
            "Sequence Length:  512 \t Batch Size:   16 \t Ms per protein 0.07\n",
            "Sequence Length:  512 \t Batch Size:   24 \t Ms per protein 0.07\n",
            "Sequence Length:  512 \t Batch Size:   32 \t Ms per protein 0.07\n",
            "************************************* Done *************************************\n",
            "*********************************** Finished ***********************************\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}